package com.wg.msshopsp.backendmanagement.shop.mapper;

import com.wg.msshopsp.backendmanagement.shop.entity.ShopP;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.mapping.StatementType;

import java.util.List;

@Mapper
public interface ShopPMapper {
    // 商品上架后，店铺的商品数+1
    @Update("UPDATE shop " +
            "SET product_count = product_count + 1 " +
            "WHERE id = #{shopId}") // #{shopId}是传入的店铺ID
    void incrementProductCount(Integer shopId);

    @Select("SELECT id AS shop_id, user_id, shop_name, shop_desc, create_time, status, logo FROM shop WHERE user_id = #{userId}")
    @Results({
            @Result(id = true, property = "id", column = "shop_id"), // 店铺ID（唯一标识）
            @Result(property = "userId", column = "user_id"),
            @Result(property = "shopName", column = "shop_name"),
            @Result(property = "shopDesc", column = "shop_desc"),
            @Result(property = "createTime", column = "create_time"),
            @Result(property = "status", column = "status"),
            @Result(property = "logo", column = "logo"),
            // 关联查询店铺下的商品（一对多）
            @Result(
                    property = "products", // 对应 ShopP 中的 products 属性
                    column = "shop_id",    // 传递店铺ID给商品查询
                    many = @Many(select = "com.wg.msshopsp.backendmanagement.shop.mapper.ProductMapper.getProductsByShopId")
            )
    })
    List<ShopP> getShopsByUserId(@Param("userId") Integer userId);





    // 新增：根据ID查询店铺
    @Select("SELECT id AS shop_id, user_id, shop_name, shop_desc, create_time, status, logo FROM shop WHERE id = #{id}")
    @Results({
            @Result(id = true, property = "id", column = "shop_id"),
            @Result(property = "userId", column = "user_id"),
            @Result(property = "shopName", column = "shop_name"),
            @Result(property = "shopDesc", column = "shop_desc"),
            @Result(property = "createTime", column = "create_time"),
            @Result(property = "status", column = "status"),
            @Result(property = "logo", column = "logo")
    })
    ShopP selectById(@Param("id") Integer id);
    // 新增：插入店铺
    @Insert("INSERT INTO shop (user_id, shop_name, shop_desc, status, logo, create_time) " +
            "VALUES (#{userId}, #{shopName}, #{shopDesc}, #{status}, #{logo}, NOW())")
    @SelectKey(
            statement = "SELECT LAST_INSERT_ID()",
            keyProperty = "id",
            resultType = Integer.class,
            before = false,
            statementType = StatementType.STATEMENT
    )
    void insert(ShopP shop);

    // 新增：更新店铺
    @Update("UPDATE shop SET " +
            "shop_name = #{shopName}, " +
            "shop_desc = #{shopDesc}, " +
            "status = #{status}, " +
            "logo = #{logo} " +
            "WHERE id = #{id}")
    void update(ShopP shop);

    // 新增：删除店铺
    @Delete("DELETE FROM shop WHERE id = #{id}")
    void deleteById(@Param("id") Integer id);


    @Select({
            "<script>",
            "SELECT s.id AS shop_id, s.user_id, s.shop_name, s.shop_desc, s.create_time, s.status, s.logo ",
            "FROM shop s",
            "LEFT JOIN product p ON s.id = p.shop_id",
            "WHERE s.user_id = #{userId}",
            // 商品名称模糊查询条件
            "<if test='productName != null and productName != \"\"'>",
            "AND p.name LIKE CONCAT('%', #{productName}, '%')",
            "</if>",
            // 商品类型查询条件
            "<if test='productType != null and productType != \"\"'>",
            "AND p.type LIKE CONCAT('%', #{productType}, '%')",
            "</if>",
            // 商品状态查询条件
            "<if test='productStatus != null and productStatus != \"\"'>",
            "AND p.status = #{productStatus}",
            "</if>",
            "GROUP BY s.id", // 避免店铺信息重复
            "</script>"
    })
    @Results({
            @Result(id = true, property = "id", column = "shop_id"),
            @Result(property = "userId", column = "user_id"),
            @Result(property = "shopName", column = "shop_name"),
            @Result(property = "shopDesc", column = "shop_desc"),
            @Result(property = "createTime", column = "create_time"),
            @Result(property = "status", column = "status"),
            @Result(property = "logo", column = "logo"),
            @Result(
                    property = "products",
                    column = "shop_id",
                    many = @Many(select = "com.wg.msshopsp.backendmanagement.shop.mapper.ProductMapper.getProductsByShopId")
            )
    })
    List<ShopP> getShopsByUserIdWithCondition(
            @Param("userId") Integer userId,
            @Param("productName") String productName,
            @Param("productType") String productType,
            @Param("productStatus") String productStatus
    );


}